/*!
 * \file      delay-board.h
 *
 * \brief     Target board delay implementation
 *
 * \copyright Revised BSD License, see section \ref LICENSE.
 *
 * \code
 *                ______                              _
 *               / _____)             _              | |
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 *               _____) ) ____| | | || |_| ____( (___| | | |
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 *              (C)2013-2017 Semtech
 *
 * \endcode
 *
 * \author    Miguel Luis ( Semtech )
 *
 * \author    Gregory Cristian ( Semtech )
 *
 * \author    Johannes Bruder ( STACKFORCE )
 */

#include "n32l43x.h"
#include "delay-board.h"

/*!
 * \brief Blocking delay of "ms" milliseconds
 *
 * \param [IN] ms    delay in milliseconds
 */
void DelayUsMcu(uint32_t us)
{
    uint32_t reload = SysTick->LOAD;
    uint32_t ticks = us * reload / 1000;
    uint32_t told, tnow, tcnt = 0;
    told = SysTick->VAL;
    while(1) {
        tnow = SysTick->VAL;
        if(tnow != told) {
            tcnt += told - tnow;
        } else {
            tcnt += reload - tnow + told;
        }
        told = tnow;
        if(tcnt >= ticks) {
            break;
        }
    }
}

/*!
 * \brief Blocking delay of "ms" milliseconds
 *
 * \param [IN] ms    delay in milliseconds
 */
void DelayMsMcu( uint32_t ms )
{
    for(;ms>0;ms--) {
        DelayUsMcu(1000);
    }
}

